package formulation;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import es.deusto.ingenieria.is.csp.algorithms.CSPAlgorithm;
import es.deusto.ingenieria.is.csp.formulation.CSPProblem;
import es.deusto.ingenieria.is.csp.formulation.Variable;

public class PbrlCryptadditionProblem extends CSPProblem<Integer> {

	public PbrlCryptadditionProblem(List<Variable<Integer>> variables, List<ArrayList<Variable<Integer>>> operation) {
		super();
		addVariables(variables);
	}

	public void solve(CSPAlgorithm<Integer> algorithm) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss.S");
		Date beginDate = Calendar.getInstance().getTime();
		System.out.println("\n* Start '" + algorithm.getClass().getSimpleName() + "' (" + formatter.format(beginDate) + ")");
		boolean solutionFound = algorithm.solve(this);
		Date endDate = Calendar.getInstance().getTime();
		System.out.println("* End   '" + algorithm.getClass().getSimpleName() + "' (" + formatter.format(endDate) + ")");

		long miliseconds = (int) Math.abs(beginDate.getTime() - endDate.getTime());
		long seconds = miliseconds / 1000;
		miliseconds %= 1000;
		long minutes = seconds / 60;
		seconds %= 60;
		long hours = minutes / 60;
		minutes %= 60;

		String time = "\n* Search lasts: ";
		time += (hours > 0) ? hours + " h " : " ";
		time += (minutes > 0) ? minutes + " m " : " ";
		time += (seconds > 0) ? seconds + "s " : " ";
		time += (miliseconds > 0) ? miliseconds + "ms " : " ";

		System.out.println(time);

		if (solutionFound) {
			System.out.println("\n- :-) Solution found!");
			System.out.println("\n" + this);
		}
		else {
			System.out.println("\n- :-( Unable to find the solution!");
		}
	}
}
